<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>

      <el-table :data="tableData">
        <el-table-column label="标题" prop="stem" width="400"></el-table-column>
        <el-table-column label="作者" prop="creator"></el-table-column>
        <el-table-column label="点赞" prop="likeCount"></el-table-column>
        <el-table-column label="浏览" prop="views"></el-table-column>
        <el-table-column label="更新时间" prop="createdAt"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <div class="actions">
              <i
                class="el-icon-view"
                @click="openDrawer('preview', row.id)"
              ></i>
              <i class="el-icon-edit" @click="openDrawer('edit', row.id)"></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="pageSize"
        :total="total"
        layout="prev, pager, next"
      >
      </el-pagination>
    </el-card>

    <el-drawer
      :title="title"
      size="60%"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="handleCloseBefore"
    >
      <div v-if="type === 'preview'" class="article-preview">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>
      <el-form
        v-else
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="请输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quillEditor>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submit">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {
  createArticle,
  updateArticle,
  getArticleDetail,
  getArticleList,
  removeArticle
} from '@/api/article'

// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'article-page',

  components: {
    quillEditor
  },

  data () {
    return {
      tableData: [],
      total: 0,
      current: 1,
      pageSize: 10,
      drawer: false,
      type: 'add',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '标题不能为空', trigger: ['blur'] }],
        content: [
          { required: true, message: '内容不能为空', trigger: ['blur'] }
        ]
      }
    }
  },
  created () {
    this.initData()
  },
  computed: {
    title () {
      // if (this.type === 'add') return '添加面经'
      if (this.type === 'preview') return '预览面经'
      if (this.type === 'edit') return '修改面经'
      return '添加面经'
    }
  },
  methods: {
    async initData () {
      const { data } = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      // console.log(res)
      this.tableData = data.rows
      this.total = data.total
    },

    async del (id) {
      // 删除请求
      await removeArticle(id)
      // 添加成功的提示
      this.$message.success('删除成功')

      // 处理删除当前页的最后一条
      if (this.tableData.length === 1 && this.current > 1) {
        this.current--
      }
      // 重新渲染
      this.initData()
    },

    async openDrawer (type, id) {
      console.log(type, id)

      this.drawer = true
      this.type = type

      if (type !== 'add') {
        const res = await getArticleDetail(id)
        this.form = {
          ...res.data
        }
      }
    },
    handleCurrentChange (val) {
      this.current = val
      this.initData()
      // setTimeout(() => {
      //   controller.abort()
      // }, 2000)
    },
    handleCloseBefore (done) {
      done()

      // this.$confirm('确定么?').then(() => {
      //   done()
      // }).catch(() => {
      //   console.log('取消')
      // })
    },
    handleClose () {
      this.form = {
        stem: '',
        content: ''
      }

      if (this.type !== 'preview') this.$refs.form.resetFields()

      this.drawer = false
    },
    async submit () {
      try {
        // 校验 (对整个表单校验)
        await this.$refs.form.validate()

        if (this.type === 'add') {
          // 请求
          await createArticle(this.form)
          // 提示
          this.$message.success('添加成功')
        }

        if (this.type === 'edit') {
          const { id, stem, content } = this.form
          await updateArticle({ id, stem, content })
          this.$message.success('编辑成功')
        }

        // 重新渲染 将当前页重置到第一页
        this.current = 1
        this.initData()
        // 关闭抽屉
        // this.drawer = false
        this.handleClose()
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
